package ru.ifmo.rybakov.muclumon.analysis2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class CombinationMultiply extends Combination {

	private Collection<Combination> combinations;
	
	public CombinationMultiply(Collection<Combination> combinations) {
		this.combinations = combinations;
	}
	
	public Function[] getFunctions() {
		// add all multiplications
		Function[] functions = null;
		for (Combination c : combinations) {
			if (functions == null) {
				functions = c.getFunctions();
			} else {
				Function[] f2 = c.getFunctions();
				ArrayList<Function> result = new ArrayList<Function>();
				for (int i = 0; i < functions.length; i++) {
					for (int j = 0; j < f2.length; j++) {
						result.add(new FunctionMultiply(functions[i], f2[j]));
					}
				}
				result.addAll(Arrays.asList(f2));
				result.addAll(Arrays.asList(functions));
				functions = new Function[result.size()];
				result.toArray(functions);
			}
		}
		
//		ArrayList<Function> result = new ArrayList<Function>(Arrays.asList(functions));
		
//		// add all functions
//		if (combinations.size() > 1)  {
//			for (Combination c : combinations) {
//				result.addAll(Arrays.asList(c.getFunctions()));
//			}
//		}
		
//		functions = new Function[result.size()];
//		result.toArray(functions);
		return functions;
	}

}
